package com.xdjd.task.service;

import com.xdjd.task.model.activity.ActivityChallengeRecordDetailPo;
import com.xdjd.task.model.activity.ActivityChallengeRecordPo;
import com.xdjd.task.service.daoservice.ActivityChallengeRecordDetailPoService;
import com.xdjd.task.service.daoservice.ActivityChallengeRecordPoService;
import com.xdjd.task.service.daoservice.MemberUserWalletPoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ChallengeService {

    @Autowired
    private ActivityChallengeRecordPoService activityChallengeRecordPoService;
    @Autowired
    private ActivityChallengeRecordDetailPoService activityChallengeRecordDetailPoService;
    @Autowired
    private MemberUserWalletPoService memberUserWalletPoService;

    public void settleChallenge() {
        activityChallengeRecordPoService.settleChallenge();
        List<ActivityChallengeRecordPo> recordPos = activityChallengeRecordPoService.findTodayRecord();
        for (ActivityChallengeRecordPo recordPo : recordPos) {
            List<ActivityChallengeRecordDetailPo> detailPos = activityChallengeRecordDetailPoService.findByRecordNo(recordPo.getRecordNo());
            for (int i = 1; i <= detailPos.size(); i++) {
                ActivityChallengeRecordDetailPo detailPo = detailPos.get(i);
                detailPo.setRanking(i);
                if (detailPo.getIsOwn().intValue() == 1) {
                    recordPo.setRanking(i);
                }
            }
            if (recordPo.getRanking().intValue() == 1) {
                recordPo.setObtainCoin(detailPos.size() - 1);
                for (int i = 1; i <= detailPos.size(); i++) {
                    ActivityChallengeRecordDetailPo detailPo = detailPos.get(i);
                    detailPo.setObtainCoin(0);
                    if (detailPo.getIsOwn().intValue() == 1) {
                        detailPo.setObtainCoin(detailPos.size() - 1);
                    }
                    activityChallengeRecordDetailPoService.updateRecordDetail(detailPo);
                    if (detailPo.getObtainCoin().intValue() > 0) {
                        memberUserWalletPoService.addObtainCoin(detailPo.getMemberId(), detailPo.getObtainCoin());
                    }
                }
            } else {
                recordPo.setObtainCoin(0);
                Integer obtainCoins = new BigDecimal(detailPos.size() - 1).divide(new BigDecimal(recordPo.getRanking() - 1), 0, BigDecimal.ROUND_DOWN).intValue();
                for (int i = 1; i <= detailPos.size(); i++) {
                    ActivityChallengeRecordDetailPo detailPo = detailPos.get(i);
                    if (i < recordPo.getRanking().intValue()) {
                        detailPo.setObtainCoin(obtainCoins);
                    } else {
                        detailPo.setObtainCoin(0);
                    }
                    activityChallengeRecordDetailPoService.updateRecordDetail(detailPo);
                    if (detailPo.getObtainCoin().intValue() > 0) {
                        memberUserWalletPoService.addObtainCoin(detailPo.getMemberId(), detailPo.getObtainCoin());
                    }
                }
            }
            activityChallengeRecordPoService.updateRecord(recordPo);
        }
    }
}
